package cn.wzut.edut.dao.inf;

import cn.wzut.edut.dao.SnowflakeIdWorker;
import cn.wzut.edut.dao.mapper.PtyClassMapper;
import cn.wzut.edut.dao.mapper.PtyCollegeMapper;
import cn.wzut.edut.dao.pojo.table.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 班级和学院管理
 */
@Repository
public class ClassCollege {
    @Autowired
    private PtyCollegeMapper ptyCollegeMapper;
    @Autowired
    private PtyClassMapper ptyClassMapper;
    @Autowired
    private Information information;
    /**
     * 查询所有班级
     */
    public List<PtyClass> selectAllClass(){return ptyClassMapper.selectList(null);}
    /**
     * 查询班级按班级id
     */
        public PtyClass selectClass(String id){
        QueryWrapper<PtyClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",id);
        return ptyClassMapper.selectOne(queryWrapper);
    }
    /**
     * 查询班级按班级名
     */
    public PtyClass selectClassInName(String class_name){
        QueryWrapper<PtyClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("class_name",class_name);
        return ptyClassMapper.selectOne(queryWrapper);
    }
    /**
     * 查询所有学院
     */
    public List<PtyCollege> selectAllCollege(){return ptyCollegeMapper.selectList(null);}
    /**
     * 查询学院按学院id
     */
    public PtyCollege selectCollege(String id){
        QueryWrapper<PtyCollege> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",id);
        return ptyCollegeMapper.selectOne(queryWrapper);
    }
    /**
     * 查询学院按学院id
     */
    public PtyCollege selectCollegeInName(String college_name){
        QueryWrapper<PtyCollege> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("college_name",college_name);
        return ptyCollegeMapper.selectOne(queryWrapper);
    }
    /**
     * 添加班级
     */
    public String insertPtyClass(PtyClass ptyClass){
        try {
            SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
            ptyClass.setId(String.valueOf(snowflakeIdWorker.nextId()));
            ptyClassMapper.insert(ptyClass);
        }catch (Exception e){
            return ptyClass.getId();
        }
        return ptyClass.getId();
    }
    /**
     * 添加学院
     */
    public String insertPtyCollege(PtyCollege ptyCollege){
        try {
            SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
            ptyCollege.setId(String.valueOf(snowflakeIdWorker.nextId()));
            ptyCollegeMapper.insert(ptyCollege);
        }catch (Exception e){
            return ptyCollege.getId();
        }
        return ptyCollege.getId();
    }
    /**
     * 更新班级
     */
    public boolean updatePtyClass(PtyClass ptyClass){
        try {
            QueryWrapper<PtyClass> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id",ptyClass.getId());
            ptyClassMapper.update(ptyClass,queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 更新学院
     */
    public boolean updatePtyCollege(PtyCollege ptyCollege){
        try {
            QueryWrapper<PtyCollege> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id",ptyCollege.getId());
            ptyCollegeMapper.update(ptyCollege,queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 删除班级按班级id
     */
    public boolean delPtyClass(String class_id){
        try {
            List<PtyStu> list1 = information.selectStuInClass(class_id);
            for (PtyStu ptyStu : list1) {
                information.delPtyStu(ptyStu.getId());
            }
            QueryWrapper<PtyClass> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id",class_id);
            ptyClassMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 删除学院按学院id
     */
    public boolean delPtyCollege(String college_id){
        try {
            List<PtyStu> list1 = information.selectStuInCollege(college_id);
            for (PtyStu ptyStu : list1) {
                information.delPtyStu(ptyStu.getId());
            }
            List<PtyTeacher> list2 = information.selectTeacherInCollege(college_id);
            for (PtyTeacher ptyTeacher : list2) {
                information.delPtyTeacher(ptyTeacher.getId());
            }
            QueryWrapper<PtyCollege> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id",college_id);
            ptyCollegeMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
}
